package Java.Lock;

//可重入锁


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SyncLockRein {
    public synchronized void add() {
        add();
    }

    public static void main(String[] args) {

        //lock演示可重入锁,显示实现有上锁就要有解锁
        Lock lock = new ReentrantLock(true);
        //创建线程
        new Thread(() -> {
            try {
                //上锁
                lock.lock();
                System.out.println(Thread.currentThread().getName() + "  外层");
                try {
                    //上锁
                    lock.lock();
                    System.out.println(Thread.currentThread().getName() + "  内层");
                } finally {
                    lock.unlock();
                }
            } finally {
                //解锁
                lock.unlock();
            }
        }, "t1").start();

        new Thread(() -> {
            lock.lock();
            System.out.println("aaaa");
            lock.unlock();
        }, "aa").start();


//        new SyncLockRein().add();
//
//       //synchronized隐私可重入锁
//        Object o=new Object();
//        new Thread(()->{
//            //代码块
//            synchronized (o){
//                System.out.println(Thread.currentThread().getName()+"  外层");
//                synchronized (o){
//                    System.out.println(Thread.currentThread().getName()+"  中层");
//                    synchronized (o){
//                        System.out.println(Thread.currentThread().getName()+"  内层");
//                    }
//                }
//            }
//        },"t1").start();
    }
}
